package com.wholesale.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wholesale.R;
import com.wholesale.beans.GoodsCategory;

public class CategoriesListAdapter extends ArrayAdapter<GoodsCategory>{

	//ListView中的数据来源
	List<GoodsCategory> catInfoList = new ArrayList<GoodsCategory>();
	private ListView listView;
	private AsyncImageLoader loader ;
	private LinearLayout loadingLayout ;
	private int lastItem ;
	public CategoriesListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView ;
		loader = AsyncImageLoader.getInstance();
	}
	public CategoriesListAdapter(Context context,List<GoodsCategory> catInfoList, ListView listView) {
		super(context, 0,catInfoList);
		this.listView = listView ;
		loader = AsyncImageLoader.getInstance();
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

        // Inflate the views from XML
        View rowView = convertView;
        CategoriesCache viewCache;
        if (rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.categoriesitem, null);
            viewCache = new CategoriesCache(rowView);
            rowView.setTag(viewCache);
        } else {
            viewCache = (CategoriesCache) rowView.getTag();
        }
        GoodsCategory catInfo = getItem(position);

        // Set the text on the TextView
        TextView textView = viewCache.getTextView();
        textView.setText(catInfo.getCatName());
        ImageView imageView = viewCache.getImageView();
        if(catInfo.getCatName().startsWith("Accessories")){
        	imageView.setBackgroundResource(R.drawable.accessories);
        }else if(catInfo.getCatName().startsWith("Bags")){
        	imageView.setBackgroundResource(R.drawable.bags);
        }else if(catInfo.getCatName().startsWith("Cosplay")){
        	imageView.setBackgroundResource(R.drawable.cosplay);
        }else if(catInfo.getCatName().startsWith("Fur")){
        	imageView.setBackgroundResource(R.drawable.fur);
        }else if(catInfo.getCatName().startsWith("Jewellery")){
        	imageView.setBackgroundResource(R.drawable.jewellery);
        }else if(catInfo.getCatName().startsWith("Kids")){
        	imageView.setBackgroundResource(R.drawable.kids);
        }else if(catInfo.getCatName().startsWith("Men")){
        	imageView.setBackgroundResource(R.drawable.men);
        }else if(catInfo.getCatName().startsWith("Sexy")){
        	imageView.setBackgroundResource(R.drawable.sexy);
        }else if(catInfo.getCatName().startsWith("Shoes")){
        	imageView.setBackgroundResource(R.drawable.shoes);
        }else if(catInfo.getCatName().startsWith("Wedding")){
        	imageView.setBackgroundResource(R.drawable.wedding);
        }else if(catInfo.getCatName().startsWith("Women")){
        	imageView.setBackgroundResource(R.drawable.women);
        }
        return rowView;
	}
	
	public void setAsyncLoadData(){
		getLoadView();
		this.listView.setOnScrollListener(scrollListener);
	}
	private void getLoadView(){
		if(loadingLayout == null){
			LinearLayout searchLayout = new LinearLayout(getContext());
			searchLayout.setOrientation(LinearLayout.HORIZONTAL);
			ProgressBar progressBar = new ProgressBar(getContext());
			progressBar.setPadding(0, 0, 15, 0);
			searchLayout.addView(progressBar,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
			searchLayout.setGravity(Gravity.CENTER);
			
			TextView loadView = new TextView(getContext());
			loadView.setText("Loading......");
			loadView.setGravity(Gravity.CENTER);
			loadView.setTextSize((float) 16.0);
			loadView.setTextColor(Color.BLACK);
			loadingLayout = new LinearLayout(getContext());
			loadingLayout.setBackgroundResource(R.drawable.categories_cell_bg_on);
			//loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
			loadingLayout.addView(loadView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
			loadingLayout.setGravity(Gravity.CENTER);
		}
	}
	
	OnScrollListener scrollListener = new OnScrollListener(){

		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			lastItem = firstVisibleItem + visibleItemCount -1;	
		}

		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub
			if(lastItem == getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE){
				loadingLayout.setVisibility(View.VISIBLE);
				listView.addFooterView(loadingLayout);
				Thread d = new Thread(new Runnable() {
					public void run() {
						// TODO Auto-generated method stub
						try {
							Thread.sleep(1500);
							mHandler.sendMessage(mHandler.obtainMessage());
						} catch (InterruptedException e) {
						}
					}
				});
				d.start();
			}
		}
		
	};
	
	Handler mHandler=new Handler(){
		public void handleMessage(Message msg){
			super.handleMessage(msg);
//			catInfoList = WebServiceClient.getCategoryLevel1();
			addItems(catInfoList);
			loadingLayout.setVisibility(View.VISIBLE);
			listView.removeFooterView(loadingLayout);
		}
	};
	/**
	 * 批量添加数据
	 * @param items
	 */
	public void addItems(List<GoodsCategory> items){
		for(GoodsCategory goodsCategory : items){
			this.add(goodsCategory);
		}
	}

}
